<template>
  <div class="card col-12">
    <div class="card-body">
      <form class="row">
        <div class="col-2">
          <input
            type="textfield"
            class="form-control"
            placeholder="公司/编号"
            v-model.trim="name"
            size="10"
          />
        </div>
        <div class="col">
          <button type="button" class="btn btn-primary ms-1" @click="search()">
            查找
          </button>
          <button
            type="button"
            class="btn btn-secondary btn-sm ms-5"
            @click="reset()"
          >
            重置
          </button>
        </div>
      </form>
    </div>
    <table class="table table-sm">
      <thead>
        <tr>
          <th>企业名称</th>
          <th>最近购买</th>
          <th>模板名称</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="info of customers">
          <td>{{ info.vipName }}</td>
          <td>{{ info.recency }}</td>
          <td>{{ info.reportTemplateName }}</td>
          <td>
            <input type="radio" v-model.number="customerId" :value="info.id" />
          </td>
        </tr>
      </tbody>
    </table>
    <div class="card-body my-0 py-0">
      <div class="row">
        <div class="col-2">
          <select v-model.number="templateId" class="form-select">
            <option :value="info.id" v-for="info of templates">
              {{ info.name }}({{ info.desc }})
            </option>
          </select>
        </div>
        <div class="col">
          <button id="btnSave" type="button" @click.stop="saveReportTemplate()" class="btn btn-primary">
            应用模板
          </button>
        </div>
      </div>
    </div>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>
  </div>
</template>

<script>
import { searchCustomerDetails } from "@/api/customer";
import { saveCustomerReportTemplate } from "@/api/admin";
import MyPagination from "@/components/my-pagination.vue";

export default {
  components: {
    MyPagination,
  },
  props: {
    templates: {
      type: Array,
    },
  },
  data() {
    return {
      name: "",
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      modalTitle: "",
      saving: false,

      customers: [],

      customerId: 0,
      templateId: 0,
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      this.$store.dispatch("showLoading", { loadingText: msg });
    },
    hideLoading: function () {
      this.$store.dispatch("hideLoading");
    },
    reset: function () {
      this.name = "";
    },
    search: function (grouped) {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.name": this.name,
        "sc.orderBy": 3,
        "sc.status": 1,
      };
      searchCustomerDetails(params, (v) => {
        this.customers = v.dataList;
        this.sc = v.page;
      });
    },
    saveReportTemplate: function () {
      const params = {
        templateId: this.templateId,
      };

      saveCustomerReportTemplate(this.customerId, params, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("保存成功");
          this.search();
        } else {
          this.showErrMsg("保存失败：" + v.errmsg, "danger");
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search(0);
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search(0);
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search(0);
    },
  },
};
</script>
